package com.faithbj.common.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.ForeignKey;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-6-3 下午08:13:55
 * 类说明
 */
@Entity
@Table(name = "user")
public class User extends BaseEntity implements UserDetails
{
	private static final long serialVersionUID = 1L;
	/**
	 * 用户实际姓名
	 */
	private String name;
	/**
	 * 登录时的用户名
	 */
	private String username;
	
	private String password;
	
	private boolean accountNonExpired;
	
	private boolean accountNonLocked;
	
	private boolean credentialsNonExpired;
	
	private boolean enabled;
	
	private Integer loginFailureCount;
	
	// 账号锁定日期
	private Date lockedDate;

	// 用户电话
	private String tel;
	
	// 用户邮箱
	private String email;
	
	// 用户住址
	private String address;
	
	// 注册IP
	private String registerIp;

	// 最后登录IP
	private String loginIp;

	// 最后登录日期
	private Date loginDate;
	
	private String operator;
	
	private Region region;
	
	private Set<Roles> roles;

	// 授权信息
	protected Collection<SimpleGrantedAuthority> authorities;
	
	@Transient
	public Collection<SimpleGrantedAuthority> getAuthorities()
	{
		List<SimpleGrantedAuthority> list=new ArrayList<SimpleGrantedAuthority>();
		for (Roles role : roles)
		{
			list.add(new SimpleGrantedAuthority(role.getValue()));
		}
	    return list;
	}

	@Column(name = "name", nullable = false)
	public String getName()
	{
		return name;
	}

	@Column(name = "password", nullable = false)
	public String getPassword()
	{
		return this.password;
	}

	@Column(name = "username", nullable = false)
	public String getUsername()
	{
		return this.username;
	}

	@Column(name = "account_nonexpired", nullable = false)
	public boolean isAccountNonExpired()
	{
		return this.accountNonExpired;
	}

	@Column(name = "account_nonlocked", nullable = false)
	public boolean isAccountNonLocked()
	{
		return this.accountNonLocked;
	}

	@Column(name = "credentials_nonexpired", nullable = false)
	public boolean isCredentialsNonExpired()
	{
		return this.credentialsNonExpired;
	}

	@Column(name = "enabled", nullable = false)
	public boolean isEnabled()
	{
		return this.enabled;
	}
	
	@Column(name = "login_failure_count")
	public Integer getLoginFailureCount()
	{
		return loginFailureCount;
	}
	
	@Column(name = "lock_date")
	public Date getLockedDate()
	{
		return lockedDate;
	}
	
	@Column(name = "tel", nullable = false)
	public String getTel()
	{
		return tel;
	}

	@Column(name = "email", nullable = false)
	public String getEmail()
	{
		return email;
	}

	@Column(name = "address", nullable = false)
	public String getAddress()
	{
		return address;
	}

	@Column(name = "register_ip")
	public String getRegisterIp()
	{
		return registerIp;
	}

	@Column(name = "login_ip")
	public String getLoginIp()
	{
		return loginIp;
	}

	@Column(name = "login_date")
	public Date getLoginDate()
	{
		return loginDate;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinColumn(nullable = false)
	@OrderBy("name asc")
	@ForeignKey(name="fk_user_role_set")
	public Set<Roles> getRoles()
	{
		return roles;
	}

	@ManyToOne(targetEntity = Region.class, fetch = FetchType.LAZY)
	@ForeignKey(name="fk_user_region")
	public Region getRegion()
	{
		return region;
	}
	
	@Column(name = "operator", nullable = false)
	public String getOperator()
	{
		return operator;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public void setAccountNonLocked(boolean accountNonLocked)
	{
		this.accountNonLocked = accountNonLocked;
	}

	public void setCredentialsNonExpired(boolean credentialsNonExpired)
	{
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public void setEnabled(boolean enabled)
	{
		this.enabled = enabled;
	}

	public void setRoles(Set<Roles> roles)
	{
		this.roles = roles;
	}

	public void setAccountNonExpired(boolean accountNonExpired)
	{
		this.accountNonExpired = accountNonExpired;
	}

	public void setLoginFailureCount(Integer loginFailureCount)
	{
		this.loginFailureCount = loginFailureCount;
	}

	public void setLockedDate(Date lockedDate)
	{
		this.lockedDate = lockedDate;
	}

	public void setRegisterIp(String registerIp)
	{
		this.registerIp = registerIp;
	}

	public void setLoginIp(String loginIp)
	{
		this.loginIp = loginIp;
	}

	public void setLoginDate(Date loginDate)
	{
		this.loginDate = loginDate;
	}

	public void setRegion(Region region)
	{
		this.region = region;
	}

	public void setAuthorities(Collection<SimpleGrantedAuthority> authorities)
	{
		this.authorities = authorities;
	}

	public void setTel(String tel)
	{
		this.tel = tel;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public void setAddress(String address)
	{
		this.address = address;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setOperator(String operator)
	{
		this.operator = operator;
	}
}
